package academic.ui.client.view;



import java.util.ArrayList;

import ui.client.GenericClickable;
import ui.client.GenericContainerTag;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.ViewImpl;

import academic.ui.client.config.CSSConstants;
import academic.ui.client.presenter.AppPresenter;
import academic.ui.client.util.ClientHelper;
import academic.ui.client.widgets.FadePanel;
import academic.ui.client.widgets.Logo;
import academic.ui.client.widgets.PickCityGraph;
import academic.ui.client.widgets.SlideDealsGraph;
import academic.ui.client.widgets.TodayDealsGraph;
import academic.ui.client.widgets.forms.PickCityForm;
import ecommerce.soco.shared.GWT_Customer;
import ecommerce.soco.shared.GWT_Product;

public class AppView extends ViewImpl implements AppPresenter.MyView{
	
	private GenericContainerTag mainPanel = new GenericContainerTag("div");
	private GenericContainerTag mainContent = new GenericContainerTag("div");
	private GenericContainerTag header = new GenericContainerTag("div");
	
	FadePanel mainHeaderPanel = new FadePanel();
	GenericContainerTag div = new GenericContainerTag("div");
	GenericClickable recentDeals = new GenericClickable("a","recent deals");
	GenericClickable userName = new GenericClickable("a");
	
	GenericClickable signInLink = new GenericClickable("a","sign in");
	GenericClickable signOutLink = new GenericClickable("a","sign out");
	GenericClickable home = new GenericClickable("a","home");
	GenericClickable deals = new GenericClickable("a","today's deals");
	
	GenericContainerTag overlay = new GenericContainerTag("div");
	GenericContainerTag giftOverlay = new GenericContainerTag("div");
	GenericContainerTag createEventOverlay = new GenericContainerTag("div");
	
	GenericContainerTag footer = new GenericContainerTag("div");
	
	//PickCityForm cityPickList = new PickCityForm();
	private GenericClickable pickCity = new GenericClickable("a","choose city");
	public Boolean isLoggedIn=false;
	
	//private Footer footer = new Footer();
	private boolean sidePanelHidden=true;
	
	private GenericClickable userNameLink = new GenericClickable("a");
	@Override
	public HasClickHandlers myProfileLink() {
		return userNameLink;
	}
	private GenericClickable myDeals = new GenericClickable("a","my deals");
	@Override
	public HasClickHandlers myDealsLink() {
		return myDeals;
	}
	private GenericClickable myGroups = new GenericClickable("a","my groups");
	@Override
	public HasClickHandlers myGiftsLink()	{
		return myGroups;
	}
	private GenericClickable myAccounts = new GenericClickable("a","my account");
	@Override
	public HasClickHandlers myAccountsLink() {
		return myAccounts;
	}

	Image avatarImg = new Image();
	private GenericContainerTag spacer = new GenericContainerTag("div");
	GenericContainerTag fbLogin = new GenericContainerTag("fb:login-button");
	private Image facebookPic = new Image();
	private SlideDealsGraph recentDealsGraph = new SlideDealsGraph();
	private TodayDealsGraph todayDealsGraph = new TodayDealsGraph();
	private PickCityGraph pickCityGraph = new PickCityGraph();
	
	@Inject
	public AppView(CSSConstants css) {
			mainPanel.setStyleName("main");
			mainPanel.add(overlay);
			mainPanel.add(giftOverlay);
			mainPanel.add(createEventOverlay);
			mainPanel.add(header);
			header.setStyleName("appviewHeader");
			mainPanel.add(mainContent);
			mainContent.setStyleName("appviewContent");
			mainPanel.add(spacer);
			mainPanel.add(footer);
			
			footer.setStyleName("footer");
			GenericContainerTag footerContent = new GenericContainerTag("div","The Deals just keep getting better!");
			footer.add(footerContent);
			footerContent.setStyleName("footerContent");
			header.add(mainHeaderPanel);
			
			overlay.setStyleName("overlay");
			giftOverlay.setStyleName("overlay");
			createEventOverlay.setStyleName("eventOverlay");
			//All for the header
			div.setStyleName(css.headerContainerCSS());
			home.getElement().setInnerHTML("home"); 
			home.getElement().setId("home");
			home.setStyleName("biglinks");
			userName.getElement().setId("homeWithTip");
			
			GenericContainerTag userToolTip = new GenericContainerTag("div");
			GenericContainerTag recentDealsTooltip = new GenericContainerTag("div");
			GenericContainerTag todayDealsTooltip = new GenericContainerTag("div");
			GenericContainerTag pickCityTooltip = new GenericContainerTag("div");
			recentDealsTooltip.add(recentDealsGraph);
			todayDealsTooltip.add(todayDealsGraph);
			pickCityTooltip.add(pickCityGraph);
			userToolTip.setStyleName("usertooltip");
			recentDealsTooltip.setStyleName("dealsTooltip");
			todayDealsTooltip.setStyleName("dealsTooltip");
			pickCityTooltip.setStyleName("dealsTooltip");
			
			GenericContainerTag userInfoDiv = new GenericContainerTag("div");
			userInfoDiv.add(avatarImg);
			userInfoDiv.add(userNameLink);
			userInfoDiv.add(myDeals );
			userInfoDiv.add(myGroups );
			userInfoDiv.add(myAccounts );
			userInfoDiv.setStyleName("userInfoDiv");
			userToolTip.add(userInfoDiv);
			avatarImg.setStyleName("avatar avatarPhoto");
			
			userName.setStyleName("userName");
			userName.addStyleName("biglinks");
			div.add(new Logo());
			GenericContainerTag linksDiv = new GenericContainerTag("div");
			linksDiv.setStyleName("linksDiv");
			div.add(linksDiv);
			linksDiv.add(userName);
			linksDiv.add(userToolTip);
			
			linksDiv.add(home);
			
			
			deals.setStyleName("longTip");
			deals.addStyleName("biglinks");
			linksDiv.add(deals);
			linksDiv.add(todayDealsTooltip);
			linksDiv.add(recentDeals);
			linksDiv.add(recentDealsTooltip);
			recentDeals.setStyleName("longTip");
			recentDeals.addStyleName("biglinks");
			
			linksDiv.add(pickCity);
			linksDiv.add(pickCityTooltip);
			
			linksDiv.add(signInLink);
			signInLink.getElement().setId(AppPresenter.SELECTOR);
			signInLink.getElement().setAttribute("rel", "#signInModal");
			signInLink.setStyleName("biglinks");
			signOutLink.getElement().setId("headerSignOutLink");
			signOutLink.setStyleName("biglinks");
			overlay.getElement().setId("signInModal");
			giftOverlay.getElement().setId("giftModal");
			createEventOverlay.getElement().setId("eventModal");
			linksDiv.add(signOutLink);
			
			
			
			userName.getElement().setInnerHTML("");
			userName.setVisible(false);
			userName.setVisible(false);
			signInLink.setVisible(true);
			signOutLink.setVisible(false);
			
			mainHeaderPanel.fadeIn();
			
			GenericContainerTag fbLoginSpan = new GenericContainerTag("span");
			fbLoginSpan.getElement().setId("fbLoginSpan");
			fbLoginSpan.add(fbLogin);
			div.add(fbLoginSpan);
			
			
			GenericContainerTag fbLoggedInSpan = new GenericContainerTag("span");
			fbLoggedInSpan.getElement().setId("fbLoggedInSpan");
			fbLoggedInSpan.add(facebookPic);
			fbLoggedInSpan.setVisible(false);
			div.add(fbLoggedInSpan);
			
			pickCity.setStyleName("longTip");
			pickCity.addStyleName("biglinks");
			
			facebookPic.setStyleName("facebookPic");
			fbLogin.getElement().setAttribute("perms","email,create_event");
			mainHeaderPanel.setAnimationEnabled(true);
			mainHeaderPanel.setContent(div);
			updateAppContentHeight();
	}
	
	@Override
	public Widget asWidget() {
		return mainPanel;
	}
	
	@Override
	public void setInSlot(Object slot, Widget w) {
		Log.debug("setInSlot:"+w.getStyleName());
		
		if(slot==AppPresenter.slot && w.getStyleName().equals("gift"))
		{
			giftOverlay.clear();
			giftOverlay.add(w);
			
		}
		else if(slot==AppPresenter.slot && w.getStyleName().equals("createEvent"))
		{
			createEventOverlay.clear();
			createEventOverlay.add(w);
		}
		else if(slot == AppPresenter.slot && w.getStyleName().equals("login"))
		{
			overlay.clear();
			overlay.add(w);
		}
		else if(slot==AppPresenter.slot)
		{
			overlay.clear();
			giftOverlay.clear();
			createEventOverlay.clear();
			mainContent.clear();
			mainContent.add(w);
		}
	}

	@Override
	public boolean sidePanelOff() {
		return sidePanelHidden;
	}

	private Integer customerID;
	
	@Override
	public void setLoginInfo(GWT_Customer customer) {
		userName.getElement().setInnerHTML(customer.getFirstName()+" "+customer.getLastName());
		if(!isLoggedIn) {
			customerID = customer.getId();
			reset();
			signInLink.setVisible(false);
			signOutLink.setVisible(true);
			userName.setVisible(true);
			home.setVisible(false);
			if(customer.getPhoto()!=null && !"".equals(customer.getPhoto()))
				avatarImg.setUrl(ClientHelper.getImageURL(customer.getPhoto(),customer.getId(),"Users"));
			else
				avatarImg.setUrl("images/blue_ghost.jpg");

			userName.setVisible(true);
			userNameLink.getElement().setInnerHTML(customer.getFirstName()+" "+ customer.getLastName());
			mainHeaderPanel.fadeIn();
			isLoggedIn=true;
		}
	}
	@Override
	public void notLoggedIn() {
		
		if(isLoggedIn) {
			reset();
			userName.getElement().setInnerHTML("");
			userName.setVisible(false);
			signInLink.setVisible(true);
			signOutLink.setVisible(false);
			home.setVisible(true);
			mainHeaderPanel.fadeIn();
			isLoggedIn=false;
		}
		
		  
	}
	
	@Override
	public native void notLoggedInFB() /*-{
		   $wnd.FB.logout(function(response) {
			   $wnd.$('#fbLoginSpan').fadeIn('slow');
		       $wnd.$('#fbLoggedInSpan').fadeOut('slow');
		       $wnd.$('#headerSignInLink').fadeIn('slow');
		       $wnd.$('#headerSignOutLink').fadeOut('slow');
		       $wnd.$('#home').fadeIn('slow');
		       $wnd.$('#homeWithTip').html('');
		       $wnd.$('#homeWithTip').fadeOut('slow');
		   });
	}-*/;

	@Override
	public HasClickHandlers signInLink() {
		return signInLink;
	}
	
	@Override
	public HasClickHandlers signOutLink() {
		return signOutLink;
	}
	
	@Override
	public HasClickHandlers home() {
		return home;
	}
	@Override
	public HasClickHandlers deals() {
		return deals;
	}
	@Override
	public HasClickHandlers myAccount() {
		return userName;
	}
	@Override
	public void reset() {
		mainHeaderPanel.fadeOut();
	}
	
	@Override
	public HasClickHandlers recentDeals() {
		return recentDeals;
	}

	/*
	@Override
	public PickCityForm getCityPicklist() {
		return cityPickList;
	}
	*/
	@Override
	public HasClickHandlers getChooseCityLink() {
		return pickCity;
	}

	@Override
	public void updateAppContentHeight() {
		//TODO: FIX Bug in IE that doesn't handle windows with scroll properly
		DOM.setStyleAttribute(mainContent.getElement(),"min-height", String.valueOf(Math.max(500, Window.getClientHeight()-150))+"px");
		DOM.setStyleAttribute(mainContent.getElement(),"height", String.valueOf(Math.max(500, Window.getClientHeight()-150))+"px");
		
	}

	
	@Override
	public void changeName(String newName) {
		userNameLink.getElement().setInnerHTML(newName);
		userName.getElement().setInnerHTML(newName);
	}
	
	@Override
	public void updateImage(String url) {
		avatarImg.setUrl(ClientHelper.getImageURL(url,customerID,"Users"));
	}

	@Override
	public native void facebookLogin() /*-{
		$wnd.FB.api('/me', function(response) {
            var query = $wnd.FB.Data.query('select name, pic, pic_square from user where uid={0}', response.id);
            query.wait(function(rows) {
              $wnd.$('#fbLoggedInSpan .facebookPic').attr('src',rows[0].pic_square);
              $wnd.$('.avatar').css('background','url('+rows[0].pic+')');
              $wnd.$('#homeWithTip').fadeIn('slow').html(rows[0].name);
            });
       });
       $wnd.$('#fbLoginSpan').fadeOut('slow');
       $wnd.$('#fbLoggedInSpan').fadeIn('slow');
       $wnd.$('#headerSignInLink').fadeOut('slow');
       $wnd.$('#headerSignOutLink').fadeIn('slow');
       $wnd.$('#home').fadeOut('slow');
	}-*/;

	@Override
	public void updateRecentDealsTooltip(ArrayList<GWT_Product> products) {
		recentDealsGraph.setup(products);
	}

	@Override
	public SlideDealsGraph getSlideDeals() {
		return recentDealsGraph;
	}
	
	@Override
	public void updateTodayDealsTooltip(ArrayList<GWT_Product> products) {
		todayDealsGraph.setup(products);
	}
	
	@Override
	public TodayDealsGraph getTodayDealsGraph() {
		return todayDealsGraph;
	}

	@Override
	public PickCityGraph getCityGraph() {
		return pickCityGraph;
	}


}
